package com.wolf.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wolf.provider.core.constant.CONSTANT;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * Json处理工具类
 * Created by sdyang on 2016/10/8.
 */
public class JsonUtil {

    /**
     * 序列化配置
     */
    private static SerializeConfig serializeConfig;

    /**
     * 反序列化配置
     */
    private static ParserConfig parserConfig;

    /**
     * 将字符串转换为一个对象
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T parseObject(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }

    /**
     * 字段值为空将被剔除；如果字段值为null，则json字符串则不会出现该字段
     *
     * @param object
     * @return
     */
    public static String toJSONString(Object object) {
        return JSON.toJSONString(object);
    }


    /**
     * 字段为空的值为空字符串；如果字段值为null，则json字符串该字段的值为""
     *
     * @param object
     * @return
     */
    public static String toJSONStringNull(Object object) {
        return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
    }

    /**
     * 将字符串转换为数组或list
     *
     * @param json
     * @param clasz
     * @param <T>
     * @return
     */
    public static <T> List<T> parseArray(String json, Class<T> clasz) {
        return JSON.parseArray(json, clasz);
    }


    /**
     * 根据策略序列化
     *
     * @param object
     * @param strategy
     * @return
     */
    public static String toJSONString(Object object, PropertyNamingStrategy strategy) {
        getSerializeConfig().propertyNamingStrategy = strategy;
        return JSON.toJSONString(object, getSerializeConfig());
    }

    /**
     * 根据策略反序列化
     *
     * @param json
     * @param clazz
     * @param strategy
     * @param <T>
     * @return
     */
    public static <T> T parseObject(String json, Class<T> clazz, PropertyNamingStrategy strategy) {
        getParserConfig().propertyNamingStrategy = strategy;
        return JSON.parseObject(json, clazz, getParserConfig());
    }

    private static SerializeConfig getSerializeConfig() {
        if (serializeConfig == null) {
            serializeConfig = new SerializeConfig();
        }
        return serializeConfig;
    }

    private static ParserConfig getParserConfig() {
        if (parserConfig == null) {
            parserConfig = new ParserConfig();
        }
        return parserConfig;
    }

    public static Map parseObject(String json) {
        return (Map) JSON.parseObject(json);
    }


    public static boolean isEmptyJSONArray(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)
                || CONSTANT.EMPTY_JSON_ARRAY.equals(jsonStr.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isEmptyJSONObject(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)
                || CONSTANT.EMPTY_JSON.equals(jsonStr.trim())) {
            return true;
        }
        JSONObject jo = JSONObject.fromObject(jsonStr);
        while (jo.keys().hasNext()) {
            Object key = jo.keys().next();
            Object o = jo.get(key);
            if (o instanceof JSONNull) {
                continue;
            }
            return false;
        }
        return true;
    }

    public static boolean isEmptyJson(String json) {
        if (org.apache.commons.lang.StringUtils.isEmpty(json)
                || "{}".equals(json.trim())) {
            return true;
        }
        return false;
    }

    public static boolean isJson(Object object) {
        try {
            JSONObject.fromObject(object.toString());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
